<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="8" :sm="24">
            <a-form-item label="用户名">
              <a-input v-model="queryParam.loginName" placeholder=""/>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24">
            <a-form-item label="手机号码">
              <a-input v-model="queryParam.userPhone" placeholder=""/>
            </a-form-item>
          </a-col>
          <template v-if="advanced">
            <a-col :md="8" :sm="24">
              <a-form-item label="昵称">
                <a-input v-model="queryParam.userName" placeholder=""/>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="用户状态">
                <a-select v-model="queryParam.userState" placeholder="请选择" :options="userStates"/>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="身份证号">
                <a-input v-model="queryParam.idCard" placeholder=""/>
              </a-form-item>
            </a-col>
          </template>
          <a-col :md="!advanced && 8 || 24" :sm="24">
            <span class="table-page-search-submitButtons"
                  :style="advanced && { float: 'right', overflow: 'hidden' } || {} ">
              <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
              <a-button style="margin-left: 8px" @click="() => queryParam = {}">重置</a-button>
              <a @click="toggleAdvanced" style="margin-left: 8px">
                {{ advanced ? '收起' : '展开' }}
                <a-icon :type="advanced ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <div class="table-operator">
      <a-button type="primary" icon="plus" @click="$refs.addUser.add()">新建</a-button>
      <a-button type="dashed" @click="tableOption">{{ optionAlertShow && '关闭' || '开启' }}提示</a-button>
      <a-dropdown v-action:edit v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item>
            <update-state-user
                :ids="selectedRowKeys.join(',')"
                :state="0"
                @ok="handleOk"/>
          </a-menu-item>
          <a-menu-item>
            <update-state-user
                :ids="selectedRowKeys.join(',')"
                :state="1"
                @ok="handleOk"/>
          </a-menu-item>
          <a-menu-item>
            <update-state-user
                :ids="selectedRowKeys.join(',')"
                :state="2"
                @ok="handleOk"/>
          </a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px">
          批量操作
          <a-icon type="down"/>
        </a-button>
      </a-dropdown>
    </div>

    <s-table
        ref="table"
        size="default"
        rowKey="key"
        :columns="columns"
        :data="loadData"
        :alert="options.alert"
        :rowSelection="options.rowSelection"
        showPagination="auto"
    >
      <span slot="userState" slot-scope="text">
        <a-tag :color="text | stateColorFilter">{{ text | stateTextFilter }}</a-tag>
      </span>
      <span slot="action" slot-scope="text, record">
        <template>
          <a @click="handleEdit(record)" :disabled="record.userState !== 0">编辑</a>
          <a-divider type="vertical"/>
          <a @click="handleDetail(record)">详情</a>
          <a-divider type="vertical"/>
          <a
              @click="handlePay(record)"
              :disabled="record.cardId === undefined || record.cardId === null">
            充值
          </a>
          <a-divider type="vertical"/>
        <a-dropdown>
          <a-menu slot="overlay">
            <a-menu-item>
              <a @click="handleResetPwd(record)" :disabled="record.userState !== 0">密码</a>
            </a-menu-item>
            <a-menu-item>
              <update-state-user
                  :ids="String(record.userId)"
                  :state="0"
                  @ok="handleOk"/>
            </a-menu-item>
            <a-menu-item>
              <update-state-user
                  :ids="String(record.userId)"
                  :state="1"
                  @ok="handleOk"/>
            </a-menu-item>
            <a-menu-item>
              <update-state-user
                  :ids="String(record.userId)"
                  :state="2"
                  @ok="handleOk"/>
            </a-menu-item>
          </a-menu>
          <a>更多<a-icon type="down"/></a>
        </a-dropdown>
        </template>
      </span>
    </s-table>
    <add-user ref="addUser" @ok="handleOk"/>
    <edit-user ref="editUser" @ok="handleOk"/>
    <pay-user ref="payUser" @ok="handleOk"/>
    <detail-user ref="detailUser" @ok="handleOk"/>
    <reset-pwd-user ref="resetPwdUser" @ok="handleOk"/>
  </a-card>
</template>

<script>
import moment from 'moment'
import {STable} from '@/components'
import {queryUser} from '@/api/basic/user'
import AddUser from './modules/user/AddUser'
import EditUser from './modules/user/EditUser'
import UpdateStateUser from './modules/user/UpdateStateUser'
import ResetPwdUser from './modules/user/ResetPwdUser'
import DetailUser from './modules/user/DetailUser'
import PayUser from "@/views/basic/modules/user/PayUser";

let that
// 用于处理过滤器动态获取数据
let stateMap = {
  0: {
    text: '使用中',
    color: 'blue'
  },
  1: {
    text: '停用',
    color: 'grey'
  },
  2: {
    text: '注销',
    color: 'red'
  },
}

export default {
  name: 'UserList',
  components: {
    PayUser,
    STable,
    AddUser,
    EditUser,
    UpdateStateUser,
    ResetPwdUser,
    DetailUser,
  },
  data () {
    return {
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParam: {},
      // 角色对象
      roles: [],
      userStates: [
        {value: 0, label: '使用中'},
        {value: 1, label: '停用'},
        {value: 2, label: '注销'},
      ],
      // 表头
      columns: [
        {
          title: '用户名',
          dataIndex: 'loginName',
          align: 'center'
        },
        {
          title: '昵称',
          dataIndex: 'userName',
          align: 'center'
        },
        {
          title: '手机号码',
          dataIndex: 'userPhone',
          align: 'center',
        },
        // {
        //   title: '信誉积分',
        //   dataIndex: 'score',
        //   align: 'center',
        //   sorter: true,
        // },
        {
          title: '用户状态',
          dataIndex: 'userState',
          align: 'center',
          sorter: true,
          scopedSlots: {customRender: 'userState'},
        },
        {
          title: '注册时间',
          dataIndex: 'gmtCreate',
          align: 'center',
          customRender: (text) => moment(text).format('YYYY-MM-DD')
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          scopedSlots: {customRender: 'action'}
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        // 统一处理查询参数，便于后端表格DTO用固定参数接收
        const queryJson = {'queryParam': JSON.stringify(this.queryParam)}
        return queryUser(Object.assign(parameter, queryJson))
            .then(res => {
              return res.result
            })
      },
      selectedRowKeys: [],
      selectedRows: [],

      // custom table alert & rowSelection
      options: {
        alert: {
          show: true, clear: () => {
            this.selectedRowKeys = []
          }
        },
        rowSelection: {
          selectedRowKeys: this.selectedRowKeys,
          onChange: this.onSelectChange
        }
      },
      optionAlertShow: false
    }
  },
  filters: {
    stateTextFilter (state) {
      if (stateMap[state] === undefined) {
        return ''
      }
      return stateMap[state].text
    },
    stateColorFilter (state) {
      if (stateMap[state] === undefined) {
        return ''
      }
      return stateMap[state].color
    },
  },
  beforeCreate () {
    that = this
  },
  created () {
    this.tableOption()
  },
  methods: {
    tableOption () {
      if (!this.optionAlertShow) {
        this.options = {
          alert: {
            show: true, clear: () => {
              this.selectedRowKeys = []
            }
          },
          rowSelection: {
            selectedRowKeys: this.selectedRowKeys,
            onChange: this.onSelectChange
          }
        }
        this.optionAlertShow = true
      } else {
        this.options = {
          alert: false,
          rowSelection: null
        }
        this.optionAlertShow = false
      }
    },
    handleEdit (record) {
      this.$refs.editUser.edit(record)
    },
    handleDetail (record) {
      this.$refs.detailUser.show(record)
    },
    handleResetPwd (record) {
      this.$refs.resetPwdUser.resetPwd(record.userId)
    },
    handlePay (record) {
      this.$refs.payUser.pay(record)
    },
    handleOk () {
      this.$refs.table.clearSelected()
      this.$refs.table.refresh(false)
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    toggleAdvanced () {
      this.advanced = !this.advanced
    }
  }
}
</script>
